package com.daxilong.starchen.player.fragment;

import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import com.daxilong.starchen.player.activity.VideoPlayerActivity;
import com.daxilong.starchen.player.adapter.VideoAdapter;
import com.daxilong.starchen.player.api.ApiService;
import com.daxilong.starchen.player.api.ApiUtils;
import com.daxilong.starchen.player.api.OnResponseData;
import com.daxilong.starchen.player.databinding.FragmentVideoBinding;
import com.daxilong.starchen.player.model.MediaItem;
import com.daxilong.starchen.player.utils.HttpUtils;
import com.daxilong.starchen.player.utils.Result;

import java.util.ArrayList;

import retrofit2.Call;


/**
 * 网路视频
 */
public class NetVideoFragment extends BaseFragment {

    private static final String TAG = "VideoFragment";
    private VideoAdapter adapter;
    private FragmentVideoBinding binding;
    private ArrayList<MediaItem> mediaItemList;

    @Override
    public View getLayout() {
        binding = FragmentVideoBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    public void initView(){
        adapter = new VideoAdapter();
        binding.recyclerView.setAdapter(adapter);

        adapter.setItemClientListener(new VideoAdapter.MyListOnClickItemListener() {
            @Override
            public void onItemClick(int position) {
                //MediaItem mediaItem = mediaItemList.get(position);

                Intent intent = new Intent(getContext(), VideoPlayerActivity.class);
                //intent.setDataAndType(Uri.parse(mediaItem.getData()),"video/*");
                Bundle bundle = new Bundle();
                bundle.putSerializable("videolist",mediaItemList);
                intent.putExtras(bundle);
                intent.putExtra("position",position);
                getContext().startActivity(intent);

            }
        });
    }

    @Override
    public void initData(){

        ApiService apiService = HttpUtils.getApiService();
        Call<Result<ArrayList<MediaItem>>> call = apiService.getNetVideo();

        ApiUtils.getNetData(getContext(), new OnResponseData<ArrayList<MediaItem>>() {
            @Override
            public void onData(ArrayList<MediaItem> data) {
                System.out.println(data);
                mediaItemList = data;
                adapter.setDataList(mediaItemList);
            }
        },call);

    }

}